package com.hangwei.aicabinet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.hangwei.aicabinet.manager.SerialPortManager;
import com.hangwei.aicabinet.manager.SerialPortProxy;
import com.hangwei.aicabinet.util.InstructionsUtil;
import com.hangwei.aicabinet.util.LogUtil;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;

/**
 * author: 张龙臣
 * date: 2018-09-06.
 */

public class SplashActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //重启应用清空指定柜门
        SPUtil.setInt(SPKey.OPENED_DOOR,0);

//        DisplayMetrics dm = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(dm);
//        int screenWidth = dm.widthPixels;
//        int screenHeigh = dm.heightPixels;
//        Log.e("DisplayMetrics","screenWidth -------"+screenWidth);
//        Log.e("DisplayMetrics","screenHeigh -------"+screenHeigh);
//
//        int[] size = new int[2];
//        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

//        Display display = wm.getDefaultDisplay();
//        Point outSize = new Point();
//        display.getSize(outSize);
//        size[0] = outSize.x;
//        size[1] = outSize.y;
//        Log.e("WindowManager","-----size[0]--------"+size[0]+"------- size[1]--"+ size[1]);

//        String[] allDevices = new SerialPortFinder().getAllDevices();
//        System.out.print(Arrays.toString(allDevices));
        openSerialPortTab();
        openSerialPortLock();
//        SystemClock.sleep(200);
//        SerialPortProxy.loopLockStatus();
//        SerialPortProxy.closeLock();

        findViewById(R.id.btnLoop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSerialPortTab();
            }
        });
        findViewById(R.id.btnLoop01).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               SerialPortProxy.setAntenna(false,1);
            }
        });
        findViewById(R.id.btnLoop02).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(false,2);
            }
        });
        findViewById(R.id.btnLoop03).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(false,3);
            }
        });
        findViewById(R.id.btnLoop04).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(false,4);
            }
        });
        findViewById(R.id.btnLoop05).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(false,5);
            }
        });
        findViewById(R.id.btnLoop06).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(InstructionsUtil.buildSetAntennaOrderMove(6));
            }
        });
        findViewById(R.id.btnLoop07).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(InstructionsUtil.buildSetAntennaOrderMove(7));
            }
        });
        findViewById(R.id.btnLoop08).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntenna(InstructionsUtil.buildSetAntennaOrderMove(8));
            }
        });
        findViewById(R.id.btnLoop09).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.setAntennaAll();
            }
        });


        findViewById(R.id.btnLoop1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSerialPortLock();
            }
        });
        findViewById(R.id.btnLoop11).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(1);
            }
        });
        findViewById(R.id.btnLoop12).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(2);
            }
        });
        findViewById(R.id.btnLoop13).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(3);
            }
        });
        findViewById(R.id.btnLoop14).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(4);
            }
        });
        findViewById(R.id.btnLoop15).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(5);
            }
        });
        findViewById(R.id.btnLoop16).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(6);
            }
        });

        findViewById(R.id.btnLoop17).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.openLock(7);
            }
        });
        findViewById(R.id.btnLoop18).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.closeLock();
            }
        });
        findViewById(R.id.btnLoop19).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.loopLockStatus();
            }
        });
        findViewById(R.id.btnLoop10).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SerialPortProxy.stopLoopLock();
            }
        });

//        startActivity(new Intent(this,AdminActivity.class));
        String id = SPUtil.getString(SPKey.CABINET_ID);
        if (TextUtils.isEmpty(id))
            startActivity(new Intent(SplashActivity.this, BindActivity.class));
        else
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        finish();
    }


    private void openSerialPortLock() {
        boolean b = SerialPortProxy.openLockDevice(new SerialPortManager.ReturnHandleCallBack() {
            @Override
            public void error(int what, String msg) {
                Log.e("LockManager", what + "   " + msg);
            }
        });
        if (b) {
            LogUtil.d("打开ttyS1成功");
        } else {
            LogUtil.e("打开ttyS1失败");
        }
    }

    private void openSerialPortTab() {
        boolean device = SerialPortProxy.openTabDevice(new SerialPortManager.ReturnHandleCallBack() {
            @Override
            public void error(int what, String msg) {
                Log.e("ReturnHandleCallBack", msg);
                if (SerialPortManager.NoResponseError.equals(msg)) {
                    ToastUtil.showCenterToast("串口无响应");
                } else {
                    switch (what) {//对应解析是什么问题导致的
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                        case 4:
                            break;
                        case 5:
                            break;
                        case 6:
                            break;
                    }
                }
            }
        });
        if (device){
            LogUtil.d("打开ttyS3成功");
            SerialPortProxy.getType();
        }
        else ToastUtil.showCenterToast("打开串口失败");
    }
}

//黑红黄
//
